﻿using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using Hxsoft.AppV1;
using Hxsoft.AppV1.Module;

namespace Hxsoft.AppV1.Admin.UserManage
{
	public partial class UserSetting : BasePage
	{
		private string _identifier;
		private bool _isSetIdentifier = false;

		/// <summary>
		/// 当前修改信息的标识符。
		/// </summary>
		protected string Identifier
		{
			get
			{
				if (!_isSetIdentifier)
				{
					_identifier = Request.QueryString["UserName"];
					_isSetIdentifier = true;
				}

				return _identifier;
			}
		}


		protected void Page_Load(object sender, EventArgs e)
		{
			if (RightValidate())
			{
				if (!IsPostBack)
				{
					InitData();
					Page.DataBind();
				}
			}
		}

		protected void BtSubmit_Click(object sender, EventArgs e)
		{
			if (RightValidate() && IsValid)
			{
				Users.SetApprovedStatus(Identifier, UCUserSetting.IsApproved);

				UpdateComplete();
			}
		}

		protected void BtnResetPassword_Click(object sender, EventArgs e)
		{
			if (RightValidate())
			{
				string newPassword = Users.ResetPassword(Identifier);

				Messages.Add(new PageMessage(true, null, PageMessageType.Prompt, Res.OperationCompleteTitle, String.Format(Res.OperationComplete_ResetPassword, newPassword)));
			}
		}

		protected void LnkbtnUnlockUser_Click(object sender, EventArgs e)
		{
			if (RightValidate())
			{
				Users.Unlock(Identifier);
				LnkbtnUnlockUser.Visible = false;

				UpdateComplete();
			}
		}

		/// <summary>
		/// 初始化数据。
		/// </summary>
		public void InitData()
		{
			User user = Users.GetUser(Identifier);

			UCUserSetting.IsApproved = user.IsApproved;
			if (user.IsLockedOut)
				LnkbtnUnlockUser.Visible = true;
			else
				LnkbtnUnlockUser.Visible = false;
		}

		/// <summary>
		/// 返回权限验证结果，如果验证失败则显示消息提示。
		/// </summary>
		/// <returns>布尔值代表权限验证是否通过。</returns>
		public bool RightValidate()
		{
			bool right = Users.GetSettingRight(Users.GetUser(Identifier));
			if (!right)
			{
				Messages.Add(new PageMessage(false, null, PageMessageType.Error, Res.PermissionExceptionMessageTitle, Res.PermissionExceptionMessage));
			}
			return right;
		}

		/// <summary>
		/// 更新操作完成，显示消息提示。
		/// </summary>
		public void UpdateComplete()
		{
			Messages.Add(new PageMessage(true, null, PageMessageType.Prompt, Res.OperationCompleteTitle, Res.OperationComplete_Setting));
		}
	}
}
